//删除本行对应班级的方法
function deleteClassRow(obj) {
    let trObj = $(obj).parents("tr");
    let tdNum = trObj.children(".tdNum").text();
    if (tdNum !== "0") {
        alert("删除失败，班级还有学生")
        return;
    }

    let tdId = trObj.children(".tdId").text();
    location.href = `removeClassBean?id=${tdId}`;
}

//检查班级是否重复
function checkIdBeforeAdd() {
    let classId = $("input[name='classId']").val();
    let url = `checkClassBean?id=${classId}`;
    $.ajax({
        type: "get",
        url: url,
        dataType: "text",
        success: function (data) {
            if (data === "true") {
                $("#classIdExistMark").show();
            }else {
                $("#classIdExistMark").hide();
            }
        },
        error: function (xhr, type, errorThrown) {
            console.log("xhr:" + xhr + " type:" + type + " errorThrown" + errorThrown);
        }
    });
}

//编辑班级信息时，让编辑信息回显到模态框
function editClassEchoValue(obj){
    let trObj = $(obj).parents("tr");
    let tdId = trObj.children(".tdId").text();
    let tdName = trObj.children(".tdName").text();
    let tdGrade = trObj.children(".tdGrade").text();
    let tdTeacher = trObj.children(".tdTeacher").text();
    let tdSlogen = trObj.children(".tdSlogen").text();

    let modalBody = $("#modalEdit .modal-body");
    modalBody.children("[name='classId']").text(tdId);
    modalBody.children("[name='className']").val(tdName);
    modalBody.children("[name='classGrade']").val(tdGrade);
    modalBody.children("[name='classTeacher']").val(tdTeacher);
    modalBody.children("[name='classSlogen']").val(tdSlogen);
}

$(function () {
    //为确定增加按钮绑定事件
    $("#addRow").click(function () {
        let modalBody = $("#modalAdd .modal-body");
        let classId = modalBody.children("[name='classId']").val();
        let className = modalBody.children("[name='className']").val();
        let classGrade = modalBody.children("[name='classGrade']").val();
        let classTeacher = modalBody.children("[name='classTeacher']").val();
        let classSlogen = modalBody.children("[name='classSlogen']").val();

        if (!classId) {
            alert("添加失败，班号不能为空且不能为非数字");
            return;
        }

        if (!className) {
            alert("添加失败，班名不能为空");
            return;
        }

        if (!classGrade) {
            alert("添加失败，年级不能为空");
            return;
        }

        if (!classTeacher) {
            alert("添加失败，老师不能为空");
            return;
        }

        if (!classSlogen) {
            alert("添加失败，口号不能为空");
            return;
        }

        //如果班号存在，会提示已存在
        if ($("#classIdExistMark").css("display") !== "none") {
            alert("添加失败，班号不能重复");
            return;
        }

        //关闭模态框，发送数据到后台服务器
        $('#modalAdd .close').click();
        location.href = `addClassBean?id=${classId}&name=${className}&grade=${classGrade}&teacher=${classTeacher}&slogen=${classSlogen}`;
    });

    //为编辑事件绑定按钮
    $("#updateRow").click(function () {
        let modalBody = $("#modalEdit .modal-body");
        let classId = modalBody.children("[name='classId']").text();/* 班号不可以编辑 */
        let className = modalBody.children("[name='className']").val();
        let classGrade = modalBody.children("[name='classGrade']").val();
        let classTeacher = modalBody.children("[name='classTeacher']").val();
        let classSlogen = modalBody.children("[name='classSlogen']").val();

        if (!classId) {
            alert("更新失败，班号信息回显失效");
            return;
        }

        if (!className) {
            alert("更新失败，班名不能为空");
            return;
        }

        if (!classGrade) {
            alert("更新失败，年级不能为空");
            return;
        }

        if (!classTeacher) {
            alert("更新失败，老师不能为空");
            return;
        }

        if (!classSlogen) {
            alert("更新失败，口号不能为空");
            return;
        }

        //关闭模态框，发送数据到后台服务器
        $('#updateRow .close').click();
        location.href = `updateClassBean?id=${classId}&name=${className}&grade=${classGrade}&teacher=${classTeacher}&slogen=${classSlogen}`;
    });
});